import 'package:flutter/material.dart';
import 'package:flutter_frame/assets/color_config.dart';
import 'package:flutter_frame/common/common_config.dart';
import 'package:flutter_frame/weiget/image_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../utils/app_util.dart';
import '../../utils/theme_util.dart';
import '../../weiget/over_scroll_behavior.dart';
import 'bean/region_model.dart';

class RegionPage extends StatefulWidget {
  const RegionPage({super.key});

  @override
  State<StatefulWidget> createState() => RegionPageState();
}

class RegionPageState extends State {
  var dataSource = [];

  @override
  void initState() {
    super.initState();
    AppUtil.getRegionList().then((list) {
      if (list.isNotEmpty) {
        setState(() {
          dataSource = list;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Select area code'),
          // toolbarHeight: 0,
          elevation: 0,
          backgroundColor: Colors.white,
          foregroundColor: Colors.transparent,
          systemOverlayStyle: ThemeUtil.systemOverlayStyleLight,
        ),
        body: ScrollConfiguration(
          behavior: OverScrollBehavior(),
          child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return itemView(dataSource[index]);
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                    padding: EdgeInsets.only(left: 10.w),
                    child: Divider(
                      color: ColorConfig.colorCCC,
                      height: 1.h,
                    ));
              },
              itemCount: dataSource.length),
        ));
  }

  itemView(RegionModel regionModel) {
    return GestureDetector(
      onTap: () => {Get.back(result: regionModel)},
      child: Container(
        height: 50.h,
        padding: EdgeInsets.symmetric(horizontal: 10.w),
        child: Row(
          children: [
            ImageWidget(
              name: "${assetImage}country/${regionModel.icon}.png",
              width: 30.w,
              fit: BoxFit.fitWidth,
            ),
            SizedBox(width: 10.w),
            Expanded(
              child: Text(
                regionModel.countryName,
                style:
                    TextStyle(color: ColorConfig.colorBlack, fontSize: 14.sp),
              ),
            ),
            Text(
              regionModel.countryCode,
              style: TextStyle(color: ColorConfig.colorBlack, fontSize: 14.sp),
            ),
          ],
        ),
      ),
    );
  }
}
